/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package izsla.entities;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 * A user entity.
 */
@Entity
@NamedQueries({
    @NamedQuery(
        name="TripUser.login",
        query="SELECT u FROM TripUser u WHERE u.name = :name AND u.password = :password"
    ),
    
    @NamedQuery(
	name="TripUser.checkName",
	query ="SELECT count(u) FROM TripUser u WHERE u.name = :name"
    ),

    @NamedQuery(
	name="TripUser.checkEmail",
	query ="SELECT count(u) FROM TripUser u WHERE u.email = :email"
    ),

    @NamedQuery(
    name="TripUser.getUsersToNotify",
    query="SELECT DISTINCT tu FROM TripUser tu, SearchCriteria sc"
    + " WHERE "
        + " tu = sc.tripUser AND"
        + " (:distance BETWEEN sc.minLength AND sc.maxLength) AND"
        + " sc.startTime <= :start AND :end <= sc.endTime"
    )

})
public class TripUser implements Serializable {
    private static final long serialVersionUID = 1L;

	/** Primary key.*/
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

	/** User name.*/
    private String name;

	/** Password's hash. */
    @Column(length=40)
    private String password;

	/** E-mail address of the user.*/
    private String email;

	/** Phone number of the user.*/
    private String phoneNumber;

    /** Is the user an admin? */
    private boolean isAdmin;

	/** Search criterias of the user.*/
    @OneToMany(mappedBy="tripUser")
    private Collection<SearchCriteria> searchCriterias;

	/** Trips organized by the user.*/
    @OneToMany(mappedBy="organiser")
    private Collection<Trip> organised;

	/** News item published by the user.*/
    @OneToMany(mappedBy="publisher")
    private Collection<News> publications;

	/** Bikes of the user.*/
    @OneToMany(mappedBy="owner")
    private Collection<Motorbike> bikes;

	/** Participation of the user.*/
    @OneToMany(mappedBy="participant")
    private Collection<Participate> trips;

	/** Images uploaded by the user.*/
    @OneToMany(mappedBy="uploader")
    private Collection<Image> images;

	/**
	 * Images uploaded by the user.
	 * @return images
	 */
    public Collection<Image> getImages() {
		return images;
    }

    public void setImages(Collection<Image> images) {
		this.images = images;
    }


    public Long getId() {
		return id;
    }

    public void setId(Long id) {
		this.id = id;
    }

    @Override
    public String toString() {
		return "izsla.entities.User[id=" + id + "]";
    }

    public String getEmail() {
		return email;
    }

    public void setEmail(String email) {
		this.email = email;
    }

    public String getName() {
		return name;
    }

    public void setName(String name) {
		this.name = name;
    }

    public boolean getIsAdmin() {
		return isAdmin;
    }

    public void setIsAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
    }

    public String getPassword() {
		return password;
    }

	/** Sets the password. The password will be hashed when the entity is persisted.
	 * @param password the unhashed password
	 */
    public void setPassword(String password) {
		this.password = password;
    }

    public String getPhoneNumber() {
		return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
    }

    public Collection<Trip> getOrganised() {
		return organised;
    }

    public Collection<Participate> getTrips() {
		return trips;
    }

    public void setTrips(Collection<Participate> trips) {
		this.trips = trips;
    }

    public Collection<News> getPublications() {
		return publications;
    }

    public Collection<Motorbike> getBikes() {
		return bikes;
    }

    public Collection<SearchCriteria> getSearchCriterias() {
		return searchCriterias;
    }

    @Override
    public boolean equals(Object obj) {
	if (obj == null) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	final TripUser other = (TripUser) obj;
		if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
			return false;
		}
		return true;
    }

    @Override
    public int hashCode() {
		int hash = (this.id != null ? this.id.hashCode() : 0);
		return hash;
    }
}
